﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Web;
//using Infrastructure.CloudWeb.Extensions;
//using Microsoft.Practices.Unity;
//using System.Web.UI;

//namespace Infrastructure.CloudWeb.WebModules
//{
//    public class UnityWebModule : IHttpModule
//    {
//        private HttpApplication context; 
//        public void Init(HttpApplication context)
//        {
//            this.context = context;
//            this.context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
//        }

//        public void Dispose() { }

//        private void OnPreRequestHandlerExecute(object sender, EventArgs e)
//        {
//            IHttpHandler currentHandler = HttpContext.Current.Handler;
//            IUnityContainer container = HttpContext.Current.Application.GetContainer();
//            if (currentHandler != null && currentHandler is Infrastructure.CloudWeb.Views.CloudWebPage)
//            { 
//                var property = currentHandler.GetType().GetProperty("Presenter");
//                if ( property !=  null) 
//                {
//                    var presenter = container.Resolve(property.PropertyType);
//                    property.SetValue(currentHandler, presenter, null);
//                }                
//            }
                
//            // User Controls are ready to be built up after page initialization is complete
//            //var currentPage = HttpContext.Current.Handler as Page;
//            //if (currentPage != null)
//            //{
//            //    currentPage.InitComplete += OnPageInitComplete;
//            //}
//        }

//        // Build up each control in the page's control tree
//        //private void OnPageInitComplete(object sender, EventArgs e)
//        //{
//        //    var currentPage = (Page)sender;
//        //    IUnityContainer container = HttpContext.Current.Application.GetContainer();
//        //    foreach (Control c in GetControlTree(currentPage))
//        //    {
//        //        if ( c is CloudWebPage)
//        //        container.BuildUp(c.GetType(), c);
//        //    }
//        //     context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute;
//        //}

//        //// Get the controls in the page's control tree excluding the page itself
//        //private IEnumerable<Control> GetControlTree(Control root)
//        //{
//        //    foreach (Control child in root.Controls)
//        //    {
//        //        yield return child;
//        //        foreach (Control c in GetControlTree(child))
//        //        {
//        //            yield return c;
//        //        }
//        //    }
//        //}


//    }
//}
